package cz.jm.web.facebook.client;

import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.user.client.Window;

public class Facebook implements EntryPoint {

	public static native void fbInit()
	/*-{
		FB.api('/me', function(response) {
			alert('Your name1 is ' + response.name);
		});
		if (typeof yourvar != 'undefined') 
			alert('NOEX 1');
	                                   
		if (window['varname'] != undefined)
			alert('NOEX 2');
	                                   
		FB.init({
			appId      : '161502117363204',        // App ID
			status     : true,           // check login status
			cookie     : true,           // enable cookies to allow the server to access the session
			xfbml      : true            // parse page for xfbml or html5 social plugins like login button below
		});
	}-*/;

	public static native void getFBUserName()
	/*-{
		FB.login(function(response) {
	    	if (response.authResponse) {		
	    		console.log('Welcome!  Fetching your information.... ');
	        	FB.api('/me', function(response) {
	        		alert('Your name1 is ' + response.name);
	        		console.log('Good to see you, ' + response.name + '.');
				});
			} 
			else {
				console.log('User cancelled login or did not fully authorize.');
			}
	    });
	}-*/;

	@Override
	public void onModuleLoad() {
		ScriptInjector.fromUrl("//connect.facebook.net/en_US/all.js")
		        .setCallback(new Callback<Void, Exception>() {
			        public void onFailure(Exception reason) {
				        Window.alert("Script load failed.");
			        }

			        public void onSuccess(Void result) {
				        Window.alert("Script load success.");
				        fbInit();
				        // getFBUserName();
			        }
		        }).inject();
	}

}
